Page({
    data: {
        btnLoading: false,
        nameText: '',
        phoneText: '',
        detailText: '',
        code: [],
        isdefault: 0,
        active: false,
        region: ['云南省', '昆明市', '五华区'],
        noteData: '选择地址',
        showPrivacy: false,
    },

    onLoad(options) {},

    onInputName(e) {
        this.setData({
            nameText: e.detail.value,
        });
    },
    clickPrivacyTab() {
        this.setData({
            showPrivacy: !this.data.showPrivacy,
        });
    },
    onInputPhone(e) {
        this.setData({
            phoneText: e.detail.value,
        });
    },

    onInputDetail(e) {
        this.setData({
            detailText: e.detail.value,
        });
    },

    onDeafault() {
        this.setData({
            active: !this.data.active,
        });
        if (this.data.active) {
            this.setData({
                isdefault: 1,
            });
        } else {
            this.setData({
                isdefault: 0,
            });
        }
    },

    bindRegionChange(e) {
        this.setData({
            noteData: e.detail.value.join('-'),
            code: e.detail.code,
        });
    },
    // 添加地址
    onAddAddress() {
        if (!this.data.nameText)
            return wx.showToast({
                title: '请输入收货人姓名',
                icon: 'none',
            });

        if (!this.data.phoneText)
            return wx.showToast({
                title: '请输入收货人手机号',
                icon: 'none',
            });

        if (this.data.phoneText.length != 11)
            return wx.showToast({
                title: '请输入正确手机号',
                icon: 'none',
            });

        if (this.data.noteData == '选择地址')
            return wx.showToast({
                title: '请选择地址',
                icon: 'none',
            });

        if (!this.data.detailText)
            return wx.showToast({
                title: '请输入详细地址',
                icon: 'none',
            });

        if (!this.data.showPrivacy)
            return wx.showToast({
                title: '请勾选同意隐私政策协议',
                icon: 'none',
            });
        this.setData({
            btnLoading: true,
        });

        this.request({
            url: 'user/my/address2/create',
            method: 'POST',
            data: {
                name: this.data.nameText,
                tel: this.data.phoneText,
                address_code: this.data.code,
                address_detail: this.data.detailText,
                is_default: this.data.isdefault,
            },
        })
            .then(async res => {
                this.setData({
                    btnLoading: false,
                });
                wx.showToast({
                    title: res.msg,
                });
                await this.sleep(1000);
                wx.navigateBack();
            })
            .catch(err => {
                this.setData({
                    btnLoading: false,
                });
            });
    },
    toPrivacy() {
        wx.navigateTo({
            url: '/pages/privacy/privacy',
        });
    },
});
